package cn.hdustea.shopNavigation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;


import com.baidu.mapapi.search.poi.PoiIndoorInfo;

import java.util.List;

import cn.hdustea.shopNavigation.R;


public class PoiSearchAdapter extends RecyclerView.Adapter<PoiSearchViewHolder> {
    private Context context;
    private List<PoiIndoorInfo> poiIndoorInfos;

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public PoiSearchAdapter(Context context, List<PoiIndoorInfo> poiIndoorInfos) {
        this.context = context;
        this.poiIndoorInfos = poiIndoorInfos;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setPoiIndoorInfos(List<PoiIndoorInfo> poiIndoorInfos) {
        this.poiIndoorInfos = poiIndoorInfos;
        notifyDataSetChanged();
    }

    public List<PoiIndoorInfo> getPoiIndoorInfos() {
        return poiIndoorInfos;
    }

    @NonNull
    @Override
    public PoiSearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PoiSearchViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.poi_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PoiSearchViewHolder holder, final int position) {
        PoiIndoorInfo poiIndoorInfo = poiIndoorInfos.get(position);
        holder.poiName.setText(poiIndoorInfo.name);
        holder.poiAddress.setText(poiIndoorInfo.address);
        holder.poiFloor.setText(poiIndoorInfo.floor);
        holder.poiTag.setText(poiIndoorInfo.tag);
        if (poiIndoorInfo.starLevel>0){
            holder.poiRating.setRating((float) ((poiIndoorInfo.starLevel) / 10.0));
        } else {
            holder.poiRating.setVisibility(View.GONE);
        }
        if (onItemClickListener != null) {
            holder.layoutPoiItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return poiIndoorInfos.size();
    }
}

class PoiSearchViewHolder extends RecyclerView.ViewHolder {
    TextView poiName;
    TextView poiAddress;
    TextView poiFloor;
    TextView poiTag;
    RatingBar poiRating;
    ConstraintLayout layoutPoiItem;

    public PoiSearchViewHolder(@NonNull View itemView) {
        super(itemView);
        poiName = itemView.findViewById(R.id.poiName);
        poiAddress = itemView.findViewById(R.id.poiAddress);
        poiFloor = itemView.findViewById(R.id.poiFloor);
        poiTag = itemView.findViewById(R.id.poiTag);
        poiRating = itemView.findViewById(R.id.poiRating);
        layoutPoiItem = itemView.findViewById(R.id.layoutPoiItem);
    }
}
